package com.car.controller.repair;


import com.car.entity.Repair;
import com.car.entity.User;
import com.car.service.repair.RepairService;
import com.car.util.PageUtil;
import com.car.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */

@Controller
public class RepairController {

    @Autowired
    RepairService repairService;

    @RequestMapping("/toRepairList")
    public ModelAndView toCarInfo() {
        ModelAndView mv = new ModelAndView("repair-List");

        return mv;
    }

    @RequestMapping("/getRepairList")
    //转为json数据
    @ResponseBody
    public Map getAllRepair(int pageNum, int pageSize, Repair repair, User u){
        //创建用于返回和查询的map
        Map<String,Object> paramsMap = new HashMap<>();
        repair.setUser(u);
        //先将查询条件封装到repair对象内
        paramsMap.put("repair",repair);
        //查询记录条数（符合模糊搜索的条件）
        int count =  repairService.getRepairCountQuery(paramsMap);
        //将记录总条数添加到map中
        paramsMap.put("count",count);
        //通过总记录条数和每页大小算出总页数
        int total = PageUtil.getTotalPage(count,pageSize);
        //将总页数添加到map中
        paramsMap.put("total",total);
        //通过页码和条数算出本次分页的起始位置
        int selectStart = pageSize*(pageNum-1);
        //将起始位置封装到map中，方便后续dao层调用
        paramsMap.put("selectStart",selectStart);
        //将每页记录条数封装到map中，方便后续dao层调用
        paramsMap.put("pageSize",pageSize);
        //通过map包含的条件 查询符合的repair组成一个List返回
        //条件包括： repair.carIdCard  repair.user.userName  模糊搜索  和  分页
        List<Repair> repairList = repairService.getAllRepairQuery(paramsMap);
        //最后将结果也封装到map中，并返回
        paramsMap.put("repairList",repairList);
        return paramsMap;
    }

    @ResponseBody
    @RequestMapping("/toAddRepair")
    public ModelAndView toAddRepair(){
        return new ModelAndView("repair-add");
    }

    @ResponseBody
    @RequestMapping("/addRepair")
    public ResultMsg addRepair(Repair repair,User user){
        ResultMsg resultMsg = new ResultMsg();
        repair.setUser(user);

        int flag =  repairService.addRepairInsert(repair);
        resultMsg.setFlag(flag);

        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/toEditRepair")
    public ModelAndView toEditRepair(Repair r){
        ModelAndView mv = new ModelAndView("repair-edit");
        Repair repair = repairService.getRepairByIdQuery(r.getRepairId());
        mv.addObject("repair",repair);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/editRepair")
    public String editRepair(Repair repair,User user){
        repair.setUser(user);
        int flag =  repairService.editRepairUpdate(repair);
        return String.valueOf(flag);
    }


    @ResponseBody
    @RequestMapping("/delRepair")
    public String delRepair(Repair repair){
        int flag =  repairService.delRepairByIdDelete(repair);
        return String.valueOf(flag);
    }


}
